<template>
  <!-- 权限点数据展示 -->
  <div>
    <el-tree
      ref="refTree"
      check-strictly
      show-checkbox
      default-expand-all
      :data="list"
      node-key="id"
      :props="{ label: 'name' }"
    />
    <el-button type="success" @click="submit">提交</el-button>
    <el-button type="info">取消</el-button>
  </div>

</template>

<script>
// setCheckedKeys + node-key
import { getPermissionList } from '@/api/permisson'
import { arrayTotree } from '@/utils/index'
import { getRoleDetail, assignPerm } from '@/api/setting'
export default {
  props: {
    roleId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: [] // 分配权限数据
    }
  },
  created() {
    this.loadPerList()
    // 根据当前的角色 id  获取角色所具有的权限点信息
    this.loadPerById()
  },
  methods: {
    async loadPerList() {
      const res = await getPermissionList()
      console.log(res.data, '获取权限表')
      this.list = arrayTotree(res.data)
    },
    // 获取角色权限点信息
    async loadPerById() {
      const res = await getRoleDetail(this.roleId)
      console.log('权限点详情', res.data)
      // 数据回填
      this.$refs.refTree.setCheckedKeys(res.data.permIds)
    },

    // 点击提交
    async submit() {
      const permIds = this.$refs.refTree.getCheckedKeys()
      // {id:角色id, permIds:[] 所有选中的节点的id组成的数组}
      const res = await assignPerm({ id: this.roleId, permIds })
      console.log(res.data)
      // 关闭弹窗
      this.$emit('success')
    }

  }
}
</script>

<style>

</style>
